﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using ApiManage.Core.Repository.MongoDB;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Rhonin.Common.Components;
using Rhonin.Common.Configurations;
using Rhonin.Common.ThirdParty.Autofac;
using Rhonin.Common.Utilities;
using Rhonin.Common.WeApi;
using Swashbuckle.AspNetCore.Swagger;

namespace ApiManage.Svc.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public static class ConfigurationExtensions
    {
        /// <summary>Register all the mvc services into ioc container.
        /// </summary>
        /// <returns></returns>
        public static Configuration RegisterMvcServices(this Configuration configuration, IServiceCollection services, IConfiguration iConfiguration)
        {
            var containerBuilder = ((AutofacObjectContainer)ObjectContainer.Current).ContainerBuilder;
            services.AddMemoryCache();
            services.AddCors(options =>
            {
                options.AddPolicy("AllowAllOrigin", builder =>
                {
                    builder.AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowAnyOrigin()
                        .AllowCredentials();
                });
            });
            services.AddMvc(option => { option.Filters.Add(new ApiExceptionFilterAttribute()); })
                .AddJsonOptions(options =>
                {
                    options.SerializerSettings.Converters.Add(new DateTimeStampConverter());
                    options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                    options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                })
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Version = "v1",
                    Title = "Api管理后台接口",
                    Description = "当前访问Ip为" + IpUtils.GetHostIp(),
                });
                //添加xml文件
                var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                var apiXmlPath = Path.Combine(basePath, "ApiManage.Svc.xml");
                var modelsXmlPath = Path.Combine(basePath, "ApiManage.Models.xml");
                c.IncludeXmlComments(apiXmlPath);
                c.IncludeXmlComments(modelsXmlPath);
                // 添加全局认证参数
                c.AddSecurityDefinition("LoginToken", new ApiKeyScheme
                {
                    Name = "Authorization",
                    In = "header",
                    Type = "apiKey",
                    Description = "登录票据"
                });
                c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
                {
                    { "LoginToken", Enumerable.Empty<string>() }
                });
                // 添加httpHeader参数
                c.OperationFilter<AddTicketHeaderParameter>();
            });
            services.AddTransient(typeof(ISysRepository<>), typeof(SysRepository<>));
            services.AddTransient(typeof(IProjectRepository<>), typeof(ProjectRepository<>));
            containerBuilder.Populate(services);
            return configuration;
        }
    }
}
